﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS.Class;

namespace QLNS.NghiepVu
{
    public partial class frmQuaTrinhDaoTao : Form
    {
        public frmQuaTrinhDaoTao()
        {
            InitializeComponent();
        }
        DB cn = new DB();
        string tag = ""; 

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            txtSoQD.Text = cn.SinhMa("SoQD","QuaTrinhDaoTao","DT");
        }
      
        private void load_combo()
        {
            cn.KetNoi();

            cmbToMon.DataSource = cn.getTable("select *from ToMon");
            cmbToMon.DisplayMember = "TenToMon";
            cmbToMon.ValueMember = "MaToMon";
        }

        private void load_grid()
        {
            cn.KetNoi();
            DataTable tbl = cn.getTable("SELECT QuaTrinhDaoTao.SoQD, QuaTrinhDaoTao.TuNgay,QuaTrinhDaoTao.DenNgay, QuaTrinhDaoTao.NoiHoc,QuaTrinhDaoTao.MaChuyenMon,ChuyenMon.TenChuyenMon,QuaTrinhDaoTao.HinhThuc FROm ChuyenMon INNER JOIN QuaTrinhDaoTao ON ChuyenMon.MaChuyenMon = QuaTrinhDaoTao.MaChuyenMon INNER JOIn NhanVien ON QuaTrinhDaoTao.MaNhanVien = dbo.NhanVien.MaNhanVien WHERE NhanVien.MaNhanVien='"+lbMaNV.Text +"'");
            dtgDaoTao.DataSource = tbl;

        }
        private void frmQuaTrinhDaoTao_Load(object sender, EventArgs e)
        {
           
            cn.KetNoi();
            tag = "";
           // load_button(tag);
            load_combo();
            load_grid();
        }
        //private void load_button(string tag)
        //{
        //    if(tag=="them")
        //    {
        //        btnThem.Enabled = false;
        //        btnSua.Enabled = false;
        //        btnXoa.Enabled = false;

        //        btnLuu.Enabled = true;
        //        btnHuy.Enabled = true;

        //        dtpDenNgay.Enabled = true;
        //        dtpTuNgay.Enabled = true;
        //        txtHinhthuc.Enabled = true;
        //        txtNoiHoc.Enabled = true;
        //        cmbChuyenMon.Enabled = true;
        //        txtNoiHoc.Text = "";
        //        txtHinhthuc.Text = "";
        //    }
        //    else if (tag == "sua")
        //    {
        //        btnThem.Enabled = false;
        //        btnSua.Enabled = false;
        //        btnXoa.Enabled = false;

        //        btnLuu.Enabled = true;
        //        btnHuy.Enabled = true;
        //        dtpDenNgay.Enabled = true;
        //        dtpTuNgay.Enabled = true;
        //        txtHinhthuc.Enabled = true;
        //        txtNoiHoc.Enabled = true;
        //        cmbChuyenMon.Enabled = true;
        //    }
        //    else
        //    {
        //        btnThem.Enabled = true;
        //        btnSua.Enabled = true;
        //        btnXoa.Enabled = true;

        //        btnHuy.Enabled = false;
        //        btnLuu.Enabled = false;
        //        txtHinhthuc.Enabled = false;
        //        txtNoiHoc.Enabled = false;
        //        txtSoQD.Enabled = false;
        //        cmbChuyenMon.Enabled = false;
        //        dtpDenNgay.Enabled = false;
        //        dtpTuNgay.Enabled = false;
                
        //    }
        //}
        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            //load_button(tag);


        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có muốn xóa không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question );
            if(h==DialogResult.Yes )
            {
                cn.KetNoi();
                cn.Delete_Table("QuaTrinhDaoTao","SoQD='"+txtSoQD.Text +"'");
                tag = "";
                load_grid();
                load_combo();
               //load_button(tag );
                MessageBox.Show("Xóa dữ liệu thành công");
            }
        }

        private void cmbToMon_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbToMon.SelectedIndex > -1)
            {
                DataTable tbl = cn.getTable("select MaNhanVien,HoTen from NhanVien, ToMon where NhanVien.MaToMon=ToMon.MaToMon and ToMon.MaToMon='"+cmbToMon.SelectedValue.ToString().Trim()+"'");
                dtgNhanVien.DataSource = tbl;
            }
        }

        private void dtgNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgNhanVien.RowCount > 0)
            {
                cmbChuyenMon.DataSource = cn.getTable("SELECT ChiTietChuyenMon.MaChuyenMon, ChuyenMon.TenChuyenMon FROM         dbo.ChiTietChuyenMon INNER JOIN ChuyenMon ON ChiTietChuyenMon.MaChuyenMon = ChuyenMon.MaChuyenMon INNER JOIN NhanVien ON ChiTietChuyenMon.MaNhanVien = NhanVien.MaNhanVien WHERE NhanVien.MaNhanVien = '"+dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString()+"'");
                cmbChuyenMon.DisplayMember = "TenChuyenMon";
                cmbChuyenMon.ValueMember = "MaChuyenMon";

                lbMaNV.Text=dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString();
                lbTenNV.Text=dtgNhanVien.CurrentRow.Cells["TenNV"].Value.ToString();
               
            }
            
        }

       
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if(tag=="them")
            {
                cn.KetNoi();
                cn.Insert_Table("QuaTrinhDaoTao","SoQD,MaNhanVien,TuNgay,DenNgay,NoiHoc,MaChuyenMon,HinhThuc","'"+txtSoQD.Text +"','"+lbMaNV.Text +"','"+dtpTuNgay.Value.ToString("yyyy-MM-dd")+"','"+dtpDenNgay.Value.ToString("yyyy-MM-dd")+"',N'"+txtNoiHoc.Text +"','"+cmbChuyenMon.SelectedValue.ToString()+"',N'"+txtHinhthuc.Text +"'");
            }
            
            else
            {
                cn.KetNoi();
                cn.Update_Table("QuaTrinhDaoTao","MaNhanVien='"+lbMaNV.Text.Trim() +"',TuNgay='"+dtpTuNgay.Value.ToString("yyyy-MM-dd")+"',DenNgay='"+dtpDenNgay.Value.ToString("yyyy-MM-dd")+"',NoiHoc=N'"+txtNoiHoc.Text +"',MaChuyenMon='"+cmbChuyenMon.SelectedValue.ToString().Trim()+"',HinhThuc=N'"+txtHinhthuc.Text +"'","SoQD='"+txtSoQD.Text +"'");
            }
            MessageBox.Show("Lưu dữ liệu thành công");
            tag = "";
            load_grid();
        }

        private void dtgDaoTao_SelectionChanged(object sender, EventArgs e)
        {
            if(dtgDaoTao.RowCount>0)
            {
                txtSoQD.Text=dtgDaoTao.CurrentRow.Cells["SoQD"].Value.ToString();
                dtpTuNgay.Value = DateTime.Parse(dtgDaoTao.CurrentRow.Cells["TuNgay"].Value.ToString());
                dtpDenNgay.Value = DateTime.Parse(dtgDaoTao.CurrentRow.Cells["DenNgay"].Value.ToString());
                cmbChuyenMon.SelectedValue=dtgDaoTao.CurrentRow.Cells["MaChuyenMon"].Value.ToString();

                txtNoiHoc.Text=dtgDaoTao.CurrentRow.Cells["NoiHoc"].Value.ToString();
                txtHinhthuc.Text=dtgDaoTao.CurrentRow.Cells["HinhThuc"].Value.ToString();

            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            tag = "";

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có muỗn xóa không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question );
            if(h==DialogResult.Yes )
            {
                this.Close();
            }
        }

        
    }
}
